// +----------------------------------------------------------------------
// | Author: dw <827400818@qq.com>
// +----------------------------------------------------------------------
// | 系统设置js代码
// +----------------------------------------------------------------------
$(function(){
    layui.use(['element', 'form'], function () {
        var element = layui.element;
        var form    = layui.form;
        // +----------------------------------------------------------------------
        // | icheck
        // +----------------------------------------------------------------------
        $('input').iCheck({
            checkboxClass: 'icheckbox_minimal-red',
            radioClass: 'iradio_minimal-red',
            increaseArea: '20%' // optional
        });//checkbox美化
        // +----------------------------------------------------------------------
        // | 弹出层
        // +----------------------------------------------------------------------
        $(".dw-dailog").click(function () {
            var dw_url    = $(this).attr("dw-url");//URL地址，必填
            var dw_title  = $(this).attr("dw-title");//弹出层标题，必填
            var dw_width  = $(this).attr("dw-width");//弹出层宽度，如80%或500px；如果没有默认为屏幕宽度的50%
            var dw_height = $(this).attr("dw-height");//弹出层高度，如50%或500px；如果没有默认为屏幕高度的50%
            if (dw_url == undefined) {
                layer.msg("请给button加上dw-url属性");
                return false;
            }
            if (dw_title == undefined) {
                layer.msg("请给button加上dw-title属性");
                return false;
            }
            if (dw_width == undefined) dw_width = '50%';
            if (dw_height == undefined) dw_height = '50%';
            layer.open({
                type: 2,
                title: dw_title,
                shadeClose: true,
                shade: 0.8,
                area: [dw_width, dw_height],
                content: dw_url,
                cancel: function (index, layero) {
                    $(".dw-refresh").trigger('click');
                    return false;
                }
            });
        });
        // +----------------------------------------------------------------------
        // | 删除
        // +----------------------------------------------------------------------
        $(".dw-delete").click(function () {
            var dw_url   = $(this).attr("dw-url");//URL地址，必填
            var dw_title = $(this).attr("dw-title");//删除数据标识，如姓名，默认为'该数据'
            if (dw_url == undefined) {
                layer.msg("请给button加上dw-url属性");
                return false;
            }
            if (dw_title == undefined) {
                dw_title = '该数据';
            }
            layer.confirm('确定要删除[' + dw_title + ']吗?', {icon: 3, title: '提示'}, function (index) {
                $.post(dw_url, {}, function (obj) {
                    if (obj.code == 200) {
                        layer.msg(obj.msg, {anim: 4, time: 2000}, function () {
                            $(".dw-refresh").trigger('click');
                        });
                    } else {
                        layer.msg(obj.msg, {anim: 1});
                    }
                });
                layer.close(index);
            });
        });
        //文本框失去焦点触发修改
        $("input[type=text]").blur(function () {
            var value = $(this).val();
            var id    = $(this).attr("data-id");
            ajaxUpdate(id, value);
        });
        //文本域失去焦点触发修改
        $("textarea").blur(function () {
            var value = $(this).val();
            var id    = $(this).attr("data-id");
            ajaxUpdate(id, value);

        });
        //icheck监听输入框的状态变为 checked
        $('input').on('ifChecked', function (event) {
            var value = $(this).val();
            var id    = $(this).attr("data-id");
            ajaxUpdate(id, value);
        });
        function ajaxUpdate(id, value) {
            $.post("update", {id: id, value: value}, function (obj) {
                if (obj.code == 1) {
                    layer.msg(obj.msg, {anim: 4, time: 2000});
                } else {
                    if (obj.msg != '修改参数值失败!') {
                        layer.msg(obj.msg, {anim: 1});
                    }
                }
            });
        }
        // +----------------------------------------------------------------------
        // | 刷新
        // +----------------------------------------------------------------------
        $(".dw-refresh").click(function () {
            location.href = location.href;
        });
    });
})
